<?php
/*
****购物车类COOKIE实现cookie('shopcart')
*/
class CartAction extends Action{
	//检查商品是否存在
	public function check_product($product_id){
		$nn=cookie('shopcart');
		if(isset($nn)){
		    $cart=unserialize(stripslashes(cookie('shopcart')));
		    for($i=0;$i<count($cart);$i++){
			    if($cart[$i]['product_id']==$product_id){
				   return $i;
			    }
		    }
		    return -2;
		}else{
			return -1;
		}
	}

	//添加到购物车
	public function add(){
		$product_id=$this->_post('id');           //商品id
		$product_num=$this->_post('num');    //商品数量
		$product=array("product_id"=>$product_id,"product_num"=>$product_num);

        //cookie('shopcart',null);
	      
		$a=$this->check_product($product_id);   //返回值a -1(无购物车session),-2(有session但无传入的商品),其他(有商品)
		if($a==-1){
			$cart[]=$product;
		}else if($a==-2){
			$cart=unserialize(stripslashes(cookie('shopcart')));
			array_push($cart,$product); 
		}else{
			$cart=unserialize(stripslashes(cookie('shopcart')));
			$cart[$a]['product_num']+=$product["product_num"];
		}
		cookie('shopcart',serialize($cart),C('CART_COOKIE'));  //设置cookie
	}

	//删除商品
	public function del(){
		$product_id=$this->_get('id');           //商品id
		$i=$this->check_product($product_id); 
		$cart=unserialize(stripslashes(cookie('shopcart')));
        if(($i!=-1)&&($i!=-2)) {
             array_splice($cart,$i,1);
        }
		cookie('shopcart',serialize($cart),C('CART_COOKIE'));  //重新保存到cookie
     }

	 //更改数量
	 public function up_num(){
		 $product_id=$this->_get('id');
		 $product_num=$this->_get('num');
		 $i=$this->check_product($product_id);
		 if(($i!=-1)&&($i!=-2)) {
		    $cart=unserialize(stripslashes(cookie('shopcart')));
			$cart[$i]['product_num']=$product_num;
		 }
		 cookie('shopcart',serialize($cart),C('CART_COOKIE'));  //重新保存到cookie
	 }

	 //清空购物车
	 public function clear_cart(){
		 cookie('shopcart',null);
	 }

	 //购物车显示
	 public function show(){
		$Products=M('Products');
		$Goods=M('Goods');
		$cart=unserialize(stripslashes(cookie('shopcart')));
		$n=count($cart);
		$total_money=0;

		for($i=0;$i<$n;$i++){
			$id=$cart[$i]['product_id'];
			$pro=$Products->field('e_products.*,e_goods.goods_id,e_goods.s_pic')->join('e_goods ON e_products.goods_id=e_goods.goods_id')->where('e_products.product_id='.$id)->find();
			$num=$cart[$i]['product_num'];

			if(($pro['store']>0)){
				if($pro['store']<$num){
					$s_sum=sprintf("%.2f", $num*$pro['price']);
					$ncart[]=array(
							"product_id"=>$id,
							"s_pic"=>$pro['s_pic'],
							"goods_id"=>$pro['goods_id'],
							"product_num"=>$pro['store'],
							"name"=>$pro['name'],
							"price"=>$pro['price'],
							"mprice"=>$pro['market_price'],
							"props"=>$pro['props'],
							"store"=>$pro['store'],
							"s_money"=>$s_sum
							);
				}else{
					$s_sum=sprintf("%.2f", $num*$pro['price']);
					$ncart[]=array(
							"product_id"=>$id,
							"s_pic"=>$pro['s_pic'],
							"goods_id"=>$pro['goods_id'],
							"product_num"=>$num,
							"name"=>$pro['name'],
							"price"=>$pro['price'],
							"mprice"=>$pro['market_price'],
							"props"=>$pro['props'],
							"store"=>$pro['store'],
							"s_money"=>$s_sum
							);
				}
		    }//if end
			$total_money+=$num*$pro['price'];
		}
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
        
		$this->total_num=$n;
		$this->total_money=sprintf("%.2f",$total_money);
		$this->cart=$ncart;
		$this->name="查看购物车";
		$this->user=$_SESSION['euname'];
		$this->cat=R('Goods/goods_cat');
		$this->display();
	 }

	 public function get_cart(){
		$Products=M('Products');
		$Goods=M('Goods');
		$cart=unserialize(stripslashes(cookie('shopcart')));
		$n=count($cart);
		$total_money=0;
		$total_weight=0;

		for($i=0;$i<$n;$i++){
			$id=$cart[$i]['product_id'];
			$pro=$Products->field('e_products.*,e_goods.goods_id,e_goods.s_pic')->join('e_goods ON e_products.goods_id=e_goods.goods_id')->where('e_products.product_id='.$id)->find();
			$num=$cart[$i]['product_num'];

			if(($pro['store']>0)){
				if($pro['store']<$num){
					$s_sum=sprintf("%.2f", $num*$pro['price']);
					$ncart[]=array(
							"product_id"=>$id,
							"s_pic"=>$pro['s_pic'],
							"goods_id"=>$pro['goods_id'],
							"product_num"=>$pro['store'],
						    "bn"=>$pro['bn'],
							"name"=>$pro['name'],
							"price"=>$pro['price'],
							"mprice"=>$pro['market_price'],
							"props"=>$pro['props'],
							"store"=>$pro['store'],
							"s_money"=>$s_sum
							);
				}else{
					$s_sum=sprintf("%.2f", $num*$pro['price']);
					$ncart[]=array(
							"product_id"=>$id,
							"s_pic"=>$pro['s_pic'],
							"goods_id"=>$pro['goods_id'],
							"product_num"=>$num,
						    "bn"=>$pro['bn'],
							"name"=>$pro['name'],
							"price"=>$pro['price'],
							"mprice"=>$pro['market_price'],
							"props"=>$pro['props'],
							"store"=>$pro['store'],
							"s_money"=>$s_sum
							);
				}
		    }//if end
			$total_money+=$num*$pro['price'];
			$total_weight+=$pro['weight'];
		}
		$_SESSION['total_weight']=$total_weight;
		$_SESSION['total_money']=sprintf("%.2f",$total_money);
		return $ncart;
	 }

}

?>